package com.wcgksoft.mobileservice.Account.mainten;


import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.ConstructionTeam.ConstructionTeam;
import com.wcgksoft.entity.mainten.Mainten;
import com.wcgksoft.entity.mainten.MaintenPhoto;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.service.ConstructionTeam.ConstructionTeamService;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.mainten.MaintenPhotoService;
import com.wcgksoft.service.mainten.MaintenService;
import com.wcgksoft.service.mainten.MaintenTypeService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 养护
 *
 * @author 
 */
@Controller
@RequestMapping("mobile/mainten")
public class AppMaintenController {



    @Autowired
    private MaintenService maintenService;
    @Autowired
    private MaintenTypeService maintenTypeService;
    @Autowired
    private GreedlandService greedlandService;

    @Autowired
    private ConstructionTeamService constructionTeamService;
    @Autowired
    private EnterpriseProjectService enterpriseProjectService;
    @Autowired
    private MaintenPhotoService maintenPhotoService;

    /**
     * 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public MsgInfo list(
            @RequestParam(value = "curPage", defaultValue = "1", required = false) Integer curPage,
            @RequestParam(value = "pageSize",defaultValue = "8", required = false) Integer pageSize,
            @RequestParam(value = "proid", required = false) String proid,
            @RequestParam(value = "areaid", required = false) String areaid,
            @RequestParam(value = "type") Integer type
           ) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Mainten.class);
        criteria.add(Restrictions.eq("departid", proid));
        criteria.add(Restrictions.eq("areaid", areaid));
        criteria.add(Restrictions.eq("type", type));
        List<Mainten> list = maintenService.findByConditions(criteria,curPage-1,pageSize);
        if(list.size()>0){
            for (int i = 0;i<list.size();i++){
                //队伍
                String duid = list.get(i).getMaintenanceTroops();
                ConstructionTeam constructionTeam = constructionTeamService.findById(Integer.valueOf(duid));
                list.get(i).setConstructionTeam(constructionTeam);
                //片区
                String areaidd = list.get(i).getAreaid();
                EnterpriseProject area = enterpriseProjectService.findById(Integer.valueOf(areaidd));
                list.get(i).setArea(area);

                DetachedCriteria criteria2 = DetachedCriteria.forClass(MaintenPhoto.class);
                criteria2.add(Restrictions.eq("maintenId",list.get(i).getId()));
                List<MaintenPhoto> byConditions = maintenPhotoService.findByConditions(criteria2);
                list.get(i).setMaintenPhotoList(byConditions);
            }



        }
        if(list.size()>0){
            msgInfo.setCode((int) Math.ceil(list.size()/8)+1);
            msgInfo.setMsg("查询成功");
            msgInfo.setData(list);
        }else{
            msgInfo.setCode(1);
            msgInfo.setMsg("无数据");
            msgInfo.setData(list);
        }
        return msgInfo;
    }

    /**
     * 概览
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="view")
    public MsgInfo view(@RequestParam(value = "id") Integer id){
        MsgInfo msgInfo = new MsgInfo();
        Mainten byId = maintenService.findById(id);

        DetachedCriteria criteria2 = DetachedCriteria.forClass(MaintenPhoto.class);
        criteria2.add(Restrictions.eq("maintenId",byId.getId()));
        List<MaintenPhoto> byConditions = maintenPhotoService.findByConditions(criteria2);
        byId.setMaintenPhotoList(byConditions);

        msgInfo.setData(byId);
        return msgInfo;
    }

}
